package abinly.harmony.permission.compiler;

import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.ParameterSpec;

import javax.lang.model.element.VariableElement;
import java.util.List;

public final class CodeBlockHelper {

    private CodeBlockHelper(){}

    public static CodeBlock makeStringArrayCodeBlock(String[] arrays) {
        CodeBlock.Builder builder = CodeBlock.builder();
        builder.add("{");
        for (int index = 0; index < arrays.length; index++) {
            if (index != 0) {
                builder.add(", $S", arrays[index]);
            } else {
                builder.add("$S", arrays[index]);
            }
        }
        builder.add("}");
        return builder.build();
    }

    public static CodeBlock makeMethodParameterCodeBlock(String prefix, List<? extends VariableElement> variableElements){
        CodeBlock.Builder builder = CodeBlock.builder();
        builder.add(prefix + "(");

        if(variableElements != null){
            for (int index = 0; index < variableElements.size(); index++) {
                VariableElement variableElement = variableElements.get(index);
                ParameterSpec parameterSpec = ParameterSpec.get(variableElement);
                if(index != 0){
                    builder.add(", $L", parameterSpec.name);
                }else{
                    builder.add("$L", parameterSpec.name);
                }
            }
        }

        builder.add(")");
        return builder.build();
    }
}
